﻿using Cloud.Core.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace Cloud.Core.Bizlogic
{
    public class LoggerWriteen
    {
        public async Task writeAsync(CloudDataBase _context, Log logger)
        {
            _context.Set<Log>().Add(logger);
            await _context.SaveChangesAsync().ConfigureAwait(false);
        }
        public async Task writeAsync(Log logger, string ContentSource)
        {
            try
            {
            var builder = new DbContextOptionsBuilder<CloudDataBase>();
            string Connection = AppConfigurtaionServices.Configuration.GetConnectionString("MySqlConnection");
            builder.UseMySql(Connection);
            var _context = new CloudDataBase(builder.Options);

            string[] arr = ContentSource.Split(',');
            Array.Reverse(arr);
            string module = arr.Join("-");
            logger.ModuleName = new Helper().ModuleName(module);
            logger.Source = module;
            _context.Set<Log>().Add(logger);
            await _context.SaveChangesAsync().ConfigureAwait(false);
            }catch (Exception ex)
            {
                string message = ex.Message;
            }

        }

        public async Task logAsync(Log logger, string path)
        {
            try
            {
            var builder = new DbContextOptionsBuilder<CloudDataBase>();
            string Connection = AppConfigurtaionServices.Configuration.GetConnectionString("MySqlConnection");
            builder.UseMySql(Connection);
            var _context = new CloudDataBase(builder.Options);

            string[] arr = path.Split('/');
            string[] d = arr.Skip(arr.Length - 2).Take(2).ToArray();
            string module = d.Join("-");
            logger.ModuleName = new Helper().ModuleName(module);
            logger.Source = module;
            _context.Set<Log>().Add(logger);
            await _context.SaveChangesAsync().ConfigureAwait(false);
            }catch(Exception ex)
            {
                string message = ex.Message;
            }

        }

        /// <summary>
        /// 重组数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="A"></typeparam>
        /// <param name="Entity"></param>
        /// <param name="Entity_A"></param>
        /// <param name="dic"></param>
        /// <returns></returns>
        public dynamic CombinedData<T, A>(T Entity, A Entity_A, Dictionary<string, string> dic)
        {
            var EntityT = System.Activator.CreateInstance<T>();
            var EntityA = System.Activator.CreateInstance<A>();
            dynamic p = new ExpandoObject();
            try
            {
                var TProps = EntityT.GetType().GetProperties();
                foreach (var item in TProps)
                {
                    var Value = Entity.GetType().GetProperty(item.Name).GetValue(Entity);
                    ((IDictionary<string, object>)p).Add(item.Name, Value);
                }
                var AProps = EntityA.GetType().GetProperties();
                var APropsNames = AProps.ToDictionary(i => i.Name, i => i.GetType());
                foreach (var kv in dic)
                {
                    if (APropsNames.Keys.Contains(kv.Key))
                    {
                        var Value = Entity_A.GetType().GetProperty(kv.Key).GetValue(Entity_A);
                        ((IDictionary<string, object>)p).Add(kv.Value, Value);
                    }
                    // key:kv.Key
                    // value: kv.Value
                }
                return p;
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                return p;
            }
        }
    }
}
